<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      Feather's website
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://feather.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class Member extends Model
{
	public $hasMany = array('Group');
	
	function login($uid, $pwd)
	{
		$this->recursive = -1;
		$this->process   = Q_PROCESS_LITE;
		$data = $this->read('id, pwd, salt', $uid);
		
		if (!empty($data))
		{
			list($salt, $pwd) = $this->cryptPwd($pwd, $data['salt']);
			if ($data['pwd'] == $pwd)
			{
				$this->Group->conditions(array('member_id' => $uid));
				$data['groups'] = $this->Group->listField('name');
				return $data;
			}
			return _('Wrong password.');
		}
		return _('There is no such account.');
	}
	
	function cryptPwd($pwd, $salt=null)
	{
		if (is_null($salt))
			$salt = substr(md5(uniqid(mt_rand(), true)), 0, 5);
		$pwd = sha1($salt.sha1($pwd));
		return array($salt, $pwd);
	}
	
	// data validation
	
	function beforeValidate()
	{
		if (empty($this->validate))
		{
			$this->validate = array(
				'id' => array(
					Valid::NotEmpty => _('A username is required to identify yourself on the site.'),
					'/^[_a-zA-Z0-9\-]{3,}$/' => _('Your username used invalid characters.'),
					'validateUnique' => _("This username isn't available, please choose another one.")
				),
				'pwd'     => array(
					Valid::NotEmpty => _('You must enter a password to protect your account.'),
					'/.{5,}/' => _('Your password must be at least be 5 characters long.')
				),
				'pwd2'    => array('validateRepeatPwd'  => _("Your passwords didn't match, please retry.")),
				'old_pwd' => array('validateCurrentPwd' => _('Wrong password.')),
				'email'   => array(
					Valid::Email     => _('A valid email is needed to contact you when needed.'),
					'validateUnique' => _('There already is an account for that email.'),
				),
			);
		}
		return true;
	}
	
	function validateUnique($field, $value)
	{
		if (is_null($this->id))
		{
			$this->conditions(array($field => $value));
			return (false !== $this->field($field, -1));
		}
		return false;
	}
	
	function validateRepeatPwd()
	{
		if ((empty($this->id) or isset($this->data['Member']['old_pwd'])) and
			$this->data['Member']['pwd'] != $this->data['Member']['pwd2'])
		{
			$this->data['Member']['pwd']  = '';
			$this->data['Member']['pwd2'] = '';
			return true;
		}
		return false;
	}
	
	function validateCurrentPwd()
	{
		$salt = $this->field('salt');
		list($salt, $pwd) = $this->cryptPwd($this->data['Member']['old_pwd'], $salt);
		
		$this->conditions(array('id' => $this->id, 'pwd' => $pwd));
		$this->recursive = -1;
		$res = $this->find('id');
		
		// wrong password?
		if (empty($res))
		{
			$this->data['Member']['old_pwd'] = '';
			$this->data['Member']['pwd'] = '';
			$this->data['Member']['pwd2'] = '';
			return true;
		}
		
		return false;
	}
	
	// overrides
	
	function beforeSave()
	{
		if (isset($this->data['Member']['pwd']))
		{
			list($salt, $pwd) = $this->cryptPwd($this->data['Member']['pwd']);
			$this->data['Member']['pwd']  = $pwd;
			$this->data['Member']['salt'] = $salt;
		}
		return true;
	}
}
?>